<template>
    <!-- 添加或修改碳排放因子数据库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px"  append-to-body :close-on-click-modal="false" custom-class="show-back-class">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :xs="24" :sm="24" :lg="24"  >
            <el-form-item label="上级分类" prop="parentId">
              <treeselect v-model="form.parentId" :options="categorizeInfoOptions" :normalizer="normalizer" @select="changeParentFun"
                          placeholder="请选择上级分类" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24"  >
            <el-form-item label="中文名称" prop="chineseName">
              <el-input v-model="form.chineseName" placeholder="请输入中文名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24"  >
            <el-form-item label="英文名称" prop="englishName">
              <el-input v-model="form.englishName" placeholder="请输入英文名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="12"  >
            <el-form-item label="分类类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择分类类型" style="width: 100%">
                <el-option class="index-option" v-for="dict in $store.state.factorDict.typeOptions"
                           :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="12"  >
            <el-form-item label="分类级别" prop="level">
              <el-select v-model="form.level" placeholder="请选择分类级别" style="width: 100%">
                <el-option class="index-option" v-for="dict in $store.state.factorDict.levelOptions"
                           :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24"  >
            <el-form-item label="排序" prop="orderNum" >
              <el-input v-model="form.orderNum" placeholder="请输入排序" @blur="verifyNumDefault(form,'orderNum',0,null)"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24"  >
            <el-form-item label="状态" prop="status" style="margin-bottom: 0">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in $store.state.factorDict.statusOptions"
                          :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { listCategorizeInfo, getCategorizeInfo, delCategorizeInfo, addCategorizeInfo, updateCategorizeInfo, exportCategorizeInfo } from "@/api/factor/categorizeInfo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "CategorizeInfo",
  props: {
    categorizeInfoOptions: {
      type: Array,
      required: true,
      default: () => []
    },
  },
  components: { Treeselect },
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [{ required: true, message: "请选择上级分类", trigger: "change" }],
        type: [{ required: true, message: "请选择分类类型", trigger: "change" }],
        level: [{ required: true, message: "请选择分类级别", trigger: "change" }],
        chineseName: [{ required: true, message: "请输入中文名称", trigger: "blur" }],
        // englishName: [{ required: true, message: "请输入英文名称", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      }
    };
  },
  created() {

  },
  methods: {
    changeParentFun(item){
      if(item.id===0){
        this.form.level=1;
        return
      }
      if(item.level===1){
        this.form.level=2;
        this.form.type=item.type;
        return
      }
      if(item.level===2){
        this.form.level=3;
        this.form.type=item.type;
        return
      }
    },
    /** 转换碳排放因子数据库数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.chineseName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: 0,
        chineseName: null,
        englishName: null,
        type: 1,
        level: 1,
        status: 1,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        orderNum: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加碳排放因子数据库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getCategorizeInfo(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改碳排放因子数据库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCategorizeInfo(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.$emit("getList");
              }
            });
          } else {
            addCategorizeInfo(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.$emit("getList");
              }
            });
          }
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
/deep/ .vue-treeselect {
  .vue-treeselect__control {
    background: #17506a !important;
    border: 1px solid #4ec3ff !important;
    color: #fff !important;
    border-radius: 4px;
  }
  .vue-treeselect__placeholder,
  .vue-treeselect__single-value {
    color: #fff !important;
  }
  .vue-treeselect__input {
    color: #fff !important;
    background: transparent !important;
  }
  .vue-treeselect__x {
    color: #fff !important;
  }
}
// 下拉菜单
/deep/ .vue-treeselect__menu {
  background: #17506a !important;
  border: 1px solid #4ec3ff !important;
  color: #fff !important;
}
// 普通选项
/deep/ .vue-treeselect__option {
  color: #fff !important;
  background: #17506a !important;
  transition: background 0.2s;
}
// 悬停时
/deep/ .vue-treeselect__option--highlight {
  background: #2496c9 !important;
  color: #fff !important;
}

// 选中项
/deep/ .vue-treeselect__option--selected {
  background: #2496c9 !important;
  color: #fff !important;
}

// 禁用项
.vue-treeselect__option--disabled {
  color: #888 !important;
  background: #17506a !important;
}
</style>